/*
 * 文件名： ScanPayServiceImpl.java
 * 
 * 工程名称: bis-service
 *
 * Gopay
 *
 * 创建日期： 2016年8月5日
 *
 * Copyright(C) 2016, by www.gopay.com.cn Inc.All rights reserved.
 *
 * 原始作者: 宣广海
 *
 */
package com.gopay.bis.service.bank.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.gopay.bis.common.util.BisPayCheckUtils;
import com.gopay.bis.manager.bank.BankOrderManager;
import com.gopay.bis.manager.bank.payconfig.BisBankExtConfigManager;
import com.gopay.bis.service.bank.BankIntrChannConfService;
import com.gopay.bis.service.bank.BankIntrChannConfService.OutStlmTranCode;
import com.gopay.bis.service.bank.BankOrderService;
import com.gopay.bis.service.bank.go.GoBankService;
import com.gopay.bis.service.bank.query.BisBankWhiteListMerchantsService;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BankOrderStatus;
import com.gopay.common.constants.bank.BankOrderType;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.BisBankInterfaceChannel;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.cps.manager.order.CpsGeneralMainOrderQueryManager;
import com.gopay.common.domain.bank.BankOrder;
import com.gopay.common.domain.bank.BankOrderExt;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.MerchantModelInfo;
import com.gopay.common.domain.bank.ScanPayBaseMessage;
import com.gopay.common.domain.bank.ScanPayRequest;
import com.gopay.common.domain.bis.BaseBisBankConfig;
import com.gopay.common.domain.bis.BaseBisBankExtConfig;
import com.gopay.common.domain.bis.BisScanPayConfig;
import com.gopay.common.domain.bis.RouteMessage;
import com.gopay.common.domain.cps.CpsGenMainOrder;
import com.gopay.common.domain.proccode.ProcCodeInfo;
import com.gopay.common.exception.GopayException;
import com.gopay.common.order.bankorder.trade.InitialSpringBeanName;
import com.gopay.common.order.bankorder.trade.TradeType;
import com.gopay.common.proccode.DicProcCodeService;
import com.gopay.common.util.spring.SpringContextUtil;
import com.gopay.remote.bank.ScanPayInteractRemoteService;
import com.gopay.remote.bank.autoroute.BankRoute;

/**
 * 第三方扫码支付、第三方APP支付业务实现
 *
 * @author 宣广海
 *
 * @version
 *
 * @since 2016年8月5日
 */
@Service("scanPayInteractService")
public class ScanPayInteractServiceImpl implements ScanPayInteractRemoteService {
	/**
	 * 定义日志对象
	 */
	private static Logger logger = LoggerFactory.getLogger(ScanPayInteractServiceImpl.class);
	/**
	 *  银行自动路由开关
	 */
	private static final String AUTO_ROUTING = "on"; 
	private static final String MERCHANTMODEL="1";
	@Resource(name = "goBankService")
	private GoBankService goBankService;

	@Resource(name = "bankIntrChannConfService")
	private BankIntrChannConfService bankIntrChannConfService;

	@Resource(name = "cpsGeneralMainOrderQueryManager")
	private CpsGeneralMainOrderQueryManager mainOrderQuery;

	@Resource(name = "bankOrderService")
	private BankOrderService bankOrderService;

	@Resource(name = "dicProcCodeService")
	private DicProcCodeService dicProcCodeService;

	@Resource(name = "bisBankExtConfigManager")
	BisBankExtConfigManager bisBankExtConfigManager;

	@Resource(name = "bankOrderManager")
	private BankOrderManager bankOrderManager;

	@Resource(name = "bisConfig")
	private Map<String, String> bisConfig;

	@Resource(name = "bankRouteService")
	private BankRoute bankRouteService;

	@Resource(name = "normalBankRouteService")
	private BankRoute normalBankRouteService;

	@Resource(name = "bisBankWhiteListMerchantsService")
	private BisBankWhiteListMerchantsService bisBankWhiteListMerchantsService;

	@Override
	public Result pay(BaseMessage msg) throws GopayException {
		logger.info("------------ScanPayInteractServiceImpl pay start-------------");
		long beginTime = System.currentTimeMillis();
		
		ScanPayRequest request = (ScanPayRequest) msg;

		logger.error("收到的请求参数键:" + ToStringBuilder.reflectionToString(request));
		Result result = new Result();
		ScanPayBaseMessage scanPayMsg = new ScanPayBaseMessage();
		try {
			/*
			 * 基础参数校验
			 */
			checkRequest(request);

			/*
			 * 构建消息对象
			 */
			BaseMessage base = goBankService.buildSign(request.getOrderId(), request.getMchCreateIp());
			BisBankBusinessProd bankBusProdEnum = BisBankBusinessProd.get(request.getBankBusProd().value);

			base.setBankBusProd(bankBusProdEnum);
			BaseBisBankExtConfig bankConfig = null;
			BisScanPayConfig bisScanConfig=null;
			if (isRouteOrWhiteList(base.getSpecial2())) {
				// 增加银行自动路由开关 add by zhengxue
				RouteMessage routeMessage = new RouteMessage();
				routeMessage.setCustId(base.getSpecial2());
				routeMessage.setGopayOrderId(base.getOrderId());
				routeMessage.setBankCode(base.getBank());
				routeMessage.setProdCode(request.getBankBusProd().value);
				routeMessage.setTransAmt(base.getAmount());

				if (IntTxnCd._00500.value.equals(base.getSpecial4())
						|| BankConstants.GOPAY_CHONGZHI_CUSTID.equals(base.getSpecial2())) {
					// 充值交易走通用方案
					bankConfig = normalBankRouteService.getNormalRoute(routeMessage);
					if (BankConstants.GOPAY_CHONGZHI_CUSTID.equals(base.getSpecial2())
							&& !IntTxnCd._00500.value.equals(base.getSpecial4())) {
						// 非充值订单获取merId为GFB0000001
						logger.error(
								base.getOrderId() + " ScanPayInteractServiceImpl支付交易获取商户ID为: " + base.getSpecial2());
					}
				} else {
					bankConfig = bankRouteService.getRoute(routeMessage);
				}
				// 记录银行自动路由日志
				bankOrderService.addBankRouteLog(routeMessage); 
			} else {
				/*
				 * 获取银行接口通道配置信息
				 */
				bankConfig = bankIntrChannConfService.findBaseBisBankExtConfig(base.getBankBusProd(), base.getBank());
			}
						
			if (BisBankInterfaceChannel._14.value.equals(bankConfig.getChCode())) {
				// 银行产品空通道
				logger.error(base.getOrderId() + " ScanPayInteractServiceImpl获取银行产品空通道");
				result.setResultCode(ProcCodeConstants.PROC_CODE_100E6090);
				result.setSuccess(false);
				return result;
			}
			
			/**
			 * 校验银行业务产品是否支持该通道
			 */
			checkBankBussProdAndIntrChan(bankConfig, request.getBankBusProd().value);
			/**
			 * 校验通道信息
			 */
			checkBankIntrChan(bankConfig, OutStlmTranCode._1000);
			
			/**
			 * 判断银行接口通道是否支持一户一码，
			 * 判断是否上送当前渠道商户所配置的商户号和密钥
			 * 都支持替换商户号和密钥，否则走默认（国付宝商户）
			 */
		
			if (BisBankBusinessProd._16.value.equals(request.getBankBusProd().value)
					&& (bankConfig.getChCode().equals(BisBankInterfaceChannel._15.value)
							|| bankConfig.getChCode().equals(BisBankInterfaceChannel._16.value)
							|| bankConfig.getChCode().equals(BisBankInterfaceChannel._28.value))
					) {
				bisScanConfig=(BisScanPayConfig)bankConfig;
				String merchantModel=bisScanConfig.getMerchantModel()==null?"":bisScanConfig.getMerchantModel();	
			if(MERCHANTMODEL.equals(merchantModel)){				
				/**
				 * 判断当前渠道是否支持一户一码
				 */
				List<MerchantModelInfo>merchantModelInfoList=request.getMerchantModel();
				logger.error("merchantModelInfoList=" + merchantModelInfoList);
				if(null!=merchantModelInfoList){
					for(MerchantModelInfo merchantModelInfo:merchantModelInfoList){
						logger.error("merchantModelInfo=" +ToStringBuilder.reflectionToString(merchantModelInfo) );
						if(bankConfig.getChCode().equals(merchantModelInfo.getChannelCode())){
							if(StringUtils.isNotBlank(merchantModelInfo.getIntrMernum())&&
									StringUtils.isNotBlank(merchantModelInfo.getIntrMerkey())){
								bankConfig.setIntrMernum1(merchantModelInfo.getIntrMernum());
								bankConfig.setIntrMerPravkeyPath(merchantModelInfo.getIntrMerkey());
								base.setMerchantModel(true);
							}						
						}
					}
				}
			}
			}
			logger.error("商户号为："+bankConfig.getIntrMernum1());
			logger.error("商户KEY为："+bankConfig.getIntrMerPravkeyPath());
			/**
			 * 平台外部清算机构代码
			 */
			String outStlmCd = InitialSpringBeanName.getOutStlmCd(bankConfig.getChCode(),
					request.getBankBusProd().value, base.getBank());
			/**
			 * 上送银行code
			 */
			String intrfaceBankCode = bankConfig.getIntrfaceBankCode();
			if (StringUtils.isBlank(intrfaceBankCode)) {
				intrfaceBankCode = base.getBank();
			}
			base.setBankIntrChann(BisBankInterfaceChannel.get(bankConfig.getChCode()));
			base.setSpecial(request.getMchCreateIp());
			base.setOutStlmCd(outStlmCd);

			
			/**
			 * 设置扫码支付信息
			 */
			BeanUtils.copyProperties(base, scanPayMsg);
			scanPayMsg.setDeviceInfo(request.getDeviceInfo());
			scanPayMsg.setMchCreateIp(request.getMchCreateIp());
			scanPayMsg.setLimitCreditPay(request.getLimitCreditPay());
			scanPayMsg.setProductId(request.getProductId());
			scanPayMsg.setGoodsBody(request.getGoodsBody());
			scanPayMsg.setGoodsTag(request.getGoodsTag());
			scanPayMsg.setBankOrderId(modifyOrderId(scanPayMsg.getOrderId(), bankConfig.getChCode()));
			/**
			 * 如果是sdk支付设置商户号和KEY
			 */
			if (BisBankBusinessProd._17.value.equals(request.getBankBusProd().value)) {
				//scanPayMsg.setMerID(request.getMerID());
				scanPayMsg.setAppId(request.getAppId());
				//scanPayMsg.setMchKey(request.getMchKey());
			}
			/**
			 * 获取订单信息与银行信息
			 */
			CpsGenMainOrder mainOrder = getMainOrder(scanPayMsg.getOrderId());
			// add by jxj 2016-11-17 民生扫码支付需要商户ID(根据商户ID查银行商户编码)
			scanPayMsg.setCustomerID(mainOrder.getMainRecvCustId());

			/**
			 * 获取SPRING加载的BeanName
			 */
			String beanName = getBeanName(mainOrder, base.getOutStlmCd(), bankConfig.getChCode());
			
			ScanPayInteractRemoteService scanPayService = (ScanPayInteractRemoteService) SpringContextUtil
					.getBean(beanName);

			if (!BisPayCheckUtils.checkExternalOrderID(base.getOrderId())) {
				// 设置一级私钥路径
				scanPayMsg.setPrivateKeyPath(bankConfig.getIntrMerPravkeyPath());
				// 设置一级私钥密码
				scanPayMsg.setPrivatePwd(bankConfig.getIntrMerPravkeyPwd());
				// 设置二级私钥路径
				scanPayMsg.setPrivateKeyPath2nd(bankConfig.getIntrMer2ndPravkeyPath());
				// 设置二级私钥密码
				scanPayMsg.setPrivatePwd2nd(bankConfig.getIntrMer2ndPravkeyPwd());
				// 设置公钥路径
				scanPayMsg.setPublicKeyPath(bankConfig.getIntrBankPubkeyPath());
				if (BisBankBusinessProd._16.value.equals(request.getBankBusProd().value)) {
					// 设置商户号
					scanPayMsg.setMerID(bankConfig.getIntrMernum1());
				}

				// 设置二级商户号
				scanPayMsg.setMerID2nd(bankConfig.getIntrMernum2());
				// 设置结算账户网点联行号
				scanPayMsg.setBranchCode(bankConfig.getBranchCode());
				// 设置银行支付地址
				scanPayMsg.setPayUrl(bankConfig.getPayUrl());
				// 设置支付后台通知地址
				scanPayMsg.setPayRuturnUrl(bankConfig.getPayRuturnUrl());
				// 设置支付后前通知地址
				scanPayMsg.setPayNotifyUrl(bankConfig.getPayNotifyUrl());
			}
			/**
			 * 国坛老窖商户先写死
			 */
			String custId=mainOrder.getMerId();
			if (BisBankBusinessProd._16.value.equals(request.getBankBusProd().value)
					&& bankConfig.getChCode().equals(BisBankInterfaceChannel._15.value) && "0000053156".equals(custId)) {
				logger.info("单独处理的商户号开始处理：="+custId);
				// 设置商户号
				scanPayMsg.setMerID("102532000704");
				// 设置一级私钥路径
				scanPayMsg.setPrivateKeyPath("aa399c1537b47fd822dda95b904808c5");
			}
			/**
			 * 校验银行订单不能为多条,此处以后修改
			 */
			List<BankOrder> bankOrders = bankOrderService.getBankOrdersByGopayOrderId(base.getOrderId());
			if (bankOrders != null && bankOrders.size() > 0) {
				result.setResultCode(ProcCodeConstants.PROC_CODE_100E5015);
				/**
				 * 中信渠道可以继续刷新二维码
				 */
				result = scanPayService.pay(scanPayMsg);
			} else {
				/**
				 * 调用支付方法
				 */
				result = scanPayService.pay(scanPayMsg);

				scanPayMsg.setBankOrderType(BankOrderType.GOPAY_BIS_ORDER);
				scanPayMsg.setBankOrderId(modifyOrderId(scanPayMsg.getOrderId(), bankConfig.getChCode()));
				logger.error("需要保存数据库base:" + ToStringBuilder.reflectionToString(scanPayMsg));
				bankOrderService.addBankOrder(scanPayMsg);
				logger.error("ScanPayInteractServiceImpl 扫码支付请求返回处理对象：" + ToStringBuilder.reflectionToString(result));
			}

		} catch (GopayException e) {
			logger.error("ScanPayInteractServiceImpl GopayException", e);
			ProcCodeInfo procCodeInfo = dicProcCodeService.getProcCodeInfo(e.getErrCode());
			result.setSuccess(false);
			result.setResultCode(e.getErrCode());
			result.setErrorMsg(procCodeInfo.getMessage());
		} catch (Exception e) {
			logger.error("ScanPayInteractServiceImpl Exception", e);
			result.setSuccess(false);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100F1002);
			result.setErrorMsg(e.getMessage());
		}

		long endTime = System.currentTimeMillis();
		logger.info("------ ScanPayInteractServiceImpl pay end，耗时(ms):" + (endTime - beginTime));
		return result;
	}

	@Override
	public Result orderQuery(BaseMessage msg) throws GopayException {
		logger.info("------------ScanPayInteractServiceImpl orderQuery start-------------");
		long beginTime = System.currentTimeMillis();
		ScanPayBaseMessage base = (ScanPayBaseMessage) msg;
		logger.error("ScanPayInteractServiceImpl 扫码支付query查询处理base对象：" + ToStringBuilder.reflectionToString(base));
		Result result = new Result();
		try {
			/**
			 * 获取订单信息
			 */
			CpsGenMainOrder mainOrder = getMainOrder(base.getOrderId());
			/**
			 * 平台外部清算机构代码
			 */
			String outStlmCd = InitialSpringBeanName.getOutStlmCd(base.getBankIntrChann().value,
					base.getBankBusProd().value, base.getBank());
			base.setOutStlmCd(outStlmCd);
			base.setAmount(mainOrder.getTotalOrderAmt());
			base.setCustomerID(mainOrder.getMerId());
			/**
			 * 获取bean
			 */
			String beanName = getBeanName(mainOrder, outStlmCd, base.getBankIntrChann().value);

			ScanPayInteractRemoteService scanPayService = (ScanPayInteractRemoteService) SpringContextUtil
					.getBean(beanName);
			if (!BisPayCheckUtils.checkExternalOrderID(base.getOrderId())) {
				// 根据银行业务产品代码、通道代码、银行代码获取通道配置信息
				BaseBisBankExtConfig bankConfig = bisBankExtConfigManager.findExtConfigByBankChannelAndBankBusProd(
						base.getBankBusProd(), base.getBankIntrChann(), base.getBank());
				/**
				 * 根据银行订单号查询订单扩展表是否存在记录，存在记录获取支付使用的商户号和商户密钥
				 */
				getBisBankOrderExt(base.getBankOrderId(),bankConfig);
				// 设置一级私钥路径
				base.setPrivateKeyPath(bankConfig.getIntrMerPravkeyPath());
				// 设置一级私钥密码
				base.setPrivatePwd(bankConfig.getIntrMerPravkeyPwd());
				// 设置二级私钥路径
				base.setPrivateKeyPath2nd(bankConfig.getIntrMer2ndPravkeyPath());
				// 设置二级私钥密码
				base.setPrivatePwd2nd(bankConfig.getIntrMer2ndPravkeyPwd());
				// 设置公钥路径
				base.setPublicKeyPath(bankConfig.getIntrBankPubkeyPath());
				if (BisBankBusinessProd._16.value.equals(base.getBankBusProd().value)) {
					// 设置商户号
					base.setMerID(bankConfig.getIntrMernum1());
				}
				// 设置二级商户号
				base.setMerID2nd(bankConfig.getIntrMernum2());
				// 设置结算账户网点联行号
				base.setBranchCode(bankConfig.getBranchCode());
				// 设置银行查询地址
				base.setQueryUrl(bankConfig.getQueryUrl());
				base.setPayUrl(bankConfig.getPayUrl());
			}
			/**
			 * 国坛老窖商户先写死
			 */
			String custId=mainOrder.getMerId();
			if (BisBankBusinessProd._16.value.equals(base.getBankBusProd().value)
					&& base.getBankIntrChann().value.equals(BisBankInterfaceChannel._15.value) && "0000053156".equals(custId)) {
				logger.info("订单查询单独处理的商户号开始处理：="+custId);
				// 设置商户号
				base.setMerID("102532000704");
				// 设置一级私钥路径
				base.setPrivateKeyPath("aa399c1537b47fd822dda95b904808c5");
			}
			/**
			 * 调用查询 方法
			 */
			result = scanPayService.orderQuery(base);
			logger.error("ScanPayInteractServiceImpl 扫码查询返回base对象：" + ToStringBuilder.reflectionToString(result));
		} catch (GopayException e) {
			logger.error("ScanPayInteractServiceImpl query error: ", e);
			result.setResultCode(e.getErrCode());
			result.setSuccess(false);
		} catch (Exception e) {
			logger.error("ScanPayInteractServiceImpl query error: ", e);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100F1002);
			result.setSuccess(false);
		}
		long endTime = System.currentTimeMillis();
		logger.info("------ ScanPayInteractServiceImpl orderQuery end，耗时(ms):" + (endTime - beginTime));
		return result;
	}

	@Override
	public Result refund(BaseMessage msg) throws GopayException {

		logger.error("ScanPayInteractServiceImpl银行退款处理base对象：" + ToStringBuilder.reflectionToString(msg));

		ScanPayBaseMessage base = new ScanPayBaseMessage();
		BeanUtils.copyProperties(msg, base);
		Result result = new Result();
		try {
			/**
			 * 获取原支付订单信息与银行信息
			 */
			CpsGenMainOrder mainOrder = getMainOrder(msg.getTkOriOrderId());
			/**
			 * 获取原支付银行订单
			 */
			List<BankOrder> bankOrders = bankOrderService.getBankOrdersByGopayOrderId(msg.getTkOriOrderId());
			BankOrder bankOrder = bankOrders.get(0);
			logger.error("退款发起  bankorder------" + ToStringBuilder.reflectionToString(bankOrder));
			if (!isSendTkOrder(bankOrder.getOutOrderId())) {
				/**
				 * 已存在处理中退款交易
				 */
				result.setResultCode(ProcCodeConstants.PROC_CODE_100E6089);
				result.setSuccess(false);
				return result;
			}
			BisBankInterfaceChannel channel = BisBankInterfaceChannel.get(bankOrder.getInterfaceChennelCode());
			BisBankBusinessProd prod = BisBankBusinessProd.get(bankOrder.getBankBizProdCode());

			/**
			 * 获取SPRING加载的BeanName
			 */

			String outStlmId = InitialSpringBeanName.getOutStlmCd(channel.value, prod.value, bankOrder.getBankCode());
			base.setOutStlmCd(outStlmId);
			/**
			 * 订单总金额
			 */
			base.setTkOriAmount(mainOrder.getTotalOrderAmt());
			String beanName = getBeanName(mainOrder, outStlmId, channel.value);
			ScanPayInteractRemoteService scanPayService = (ScanPayInteractRemoteService) SpringContextUtil
					.getBean(beanName);		
			base.setBankOrderId(modifyOrderId(base.getOrderId(), channel.value));
			base.setTkOriBankOrderId(bankOrder.getOutOrderId());
			base.setBankBusProd(BisBankBusinessProd._12);
			base.setBankIntrChann(channel);
			base.setBank(bankOrder.getBankCode());
			base.setBankOrderType(BankOrderType.GOPAY_BIS_ORDER);
			base.setCustomerID(mainOrder.getMerId());
			if (!BisPayCheckUtils.checkExternalOrderID(base.getOrderId())) {
				// 根据银行业务产品代码、通道代码、银行代码获取通道配置信息
				BaseBisBankExtConfig bankConfig = bisBankExtConfigManager.findExtConfigByBankChannelAndBankBusProd(prod,
						channel, base.getBank());
				/**
				 * 根据银行订单号查询订单扩展表是否存在记录，存在记录获取支付使用的商户号和商户密钥
				 */
				getBisBankOrderExt(bankOrder.getOutOrderId(),bankConfig);
				// 设置一级私钥路径
				base.setPrivateKeyPath(bankConfig.getIntrMerPravkeyPath());
				// 设置一级私钥密码
				base.setPrivatePwd(bankConfig.getIntrMerPravkeyPwd());
				// 设置二级私钥路径
				base.setPrivateKeyPath2nd(bankConfig.getIntrMer2ndPravkeyPath());
				// 设置二级私钥密码
				base.setPrivatePwd2nd(bankConfig.getIntrMer2ndPravkeyPwd());
				// 设置公钥路径
				base.setPublicKeyPath(bankConfig.getIntrBankPubkeyPath());
				if (BisBankBusinessProd._16.value.equals(prod.value)) {
					// 设置商户号
					base.setMerID(bankConfig.getIntrMernum1());
				}
				// 设置二级商户号
				base.setMerID2nd(bankConfig.getIntrMernum2());
				// 设置结算账户网点联行号
				base.setBranchCode(bankConfig.getBranchCode());
				// 设置银行查询地址
				base.setQueryUrl(bankConfig.getQueryUrl());
				// 设置银行支付地址
				base.setPayUrl(bankConfig.getPayUrl());
			}
			/**
			 * 国坛老窖商户先写死
			 */
			String custId=mainOrder.getMerId();
			if (BisBankBusinessProd._16.value.equals(prod.value)
					&& channel.value.equals(BisBankInterfaceChannel._15.value) && "0000053156".equals(custId)) {
				logger.info("线上退款单独处理的商户号开始处理：="+custId);
				// 设置商户号
				base.setMerID("102532000704");
				// 设置一级私钥路径
				base.setPrivateKeyPath("aa399c1537b47fd822dda95b904808c5");
			}
			// 退款
			result = scanPayService.refund(base);
			logger.error("ScanPayInteractServiceImpl银行退款返回base对象：" + ToStringBuilder.reflectionToString(result));
		} catch (GopayException e) {
			logger.error("ScanPayInteractServiceImpl refund error: ", e);
			result.setResultCode(e.getErrCode());
			result.setSuccess(false);
		} catch (Exception e) {
			logger.error("ScanPayInteractServiceImpl refund error: ", e);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100F1002);
			result.setSuccess(false);
		}
		return result;
	}

	@Override
	public Result refundQuery(BaseMessage msg) throws GopayException {

		Result result = new Result();
		ScanPayBaseMessage base = (ScanPayBaseMessage) msg;
		try {
			logger.error("ScanPayInteractServiceImpl退款查询处理base对象：" + ToStringBuilder.reflectionToString(base));
			/**
			 * 获取原支付银行订单
			 */
			BankOrder bankOrder = bankOrderService.getBankOrderById(msg.getTkOriBankOrderId());
			/**
			 * 获取原支付订单
			 */
			CpsGenMainOrder mainOrder = getMainOrder(bankOrder.getOrderId());
			BisBankInterfaceChannel channel = BisBankInterfaceChannel.get(bankOrder.getInterfaceChennelCode());
			BisBankBusinessProd prod = BisBankBusinessProd.get(bankOrder.getBankBizProdCode());
			/**
			 * 获取SPRING加载的BeanName
			 */
			String outStlmId = InitialSpringBeanName.getOutStlmCd(channel.value, prod.value, base.getBank());
			base.setOutStlmCd(outStlmId);
			// base.setAmount(mainOrder.getTotalOrderAmt());
			base.setTkOriBankOrderId(bankOrder.getOutOrderId());
			base.setCustomerID(mainOrder.getMerId());
			base.setBankIntrChann(channel);
			//平安退款订单不返回退款金额
			if(BisBankInterfaceChannel._25.value.equals(channel.value)){
				CpsGenMainOrder refundOrder = getMainOrder(base.getOrderId());
				base.setAmount(refundOrder.getMerTxnAmt());
			}
			
			String beanName = getBeanName(mainOrder, outStlmId, channel.value);
			ScanPayInteractRemoteService scanPayService = (ScanPayInteractRemoteService) SpringContextUtil
					.getBean(beanName);

			if (!BisPayCheckUtils.checkExternalOrderID(base.getOrderId())) {
				// 根据银行业务产品代码、通道代码、银行代码获取通道配置信息
				BaseBisBankExtConfig bankConfig = bisBankExtConfigManager.findExtConfigByBankChannelAndBankBusProd(prod,
						channel, base.getBank());
				/**
				 * 根据银行订单号查询订单扩展表是否存在记录，存在记录获取支付使用的商户号和商户密钥
				 */
				getBisBankOrderExt(bankOrder.getOutOrderId(),bankConfig);
				// 设置一级私钥路径
				base.setPrivateKeyPath(bankConfig.getIntrMerPravkeyPath());
				// 设置一级私钥密码
				base.setPrivatePwd(bankConfig.getIntrMerPravkeyPwd());
				// 设置二级私钥路径
				base.setPrivateKeyPath2nd(bankConfig.getIntrMer2ndPravkeyPath());
				// 设置二级私钥密码
				base.setPrivatePwd2nd(bankConfig.getIntrMer2ndPravkeyPwd());
				// 设置公钥路径
				base.setPublicKeyPath(bankConfig.getIntrBankPubkeyPath());
				if (BisBankBusinessProd._16.value.equals(prod.value)) {
					// 设置商户号
					base.setMerID(bankConfig.getIntrMernum1());
				}
				// 设置二级商户号
				base.setMerID2nd(bankConfig.getIntrMernum2());
				// 设置结算账户网点联行号
				base.setBranchCode(bankConfig.getBranchCode());
				// 设置银行查询地址
				base.setQueryUrl(bankConfig.getQueryUrl());
				base.setPayUrl(bankConfig.getPayUrl());
			}
			/**
			 * 国坛老窖商户先写死
			 */
			String custId=mainOrder.getMerId();
			if (BisBankBusinessProd._16.value.equals(prod.value)
					&& channel.value.equals(BisBankInterfaceChannel._15.value) && "0000053156".equals(custId)) {
				logger.info("线上退款查询单独处理的商户号开始处理：="+custId);
				// 设置商户号
				base.setMerID("102532000704");
				// 设置一级私钥路径
				base.setPrivateKeyPath("aa399c1537b47fd822dda95b904808c5");
			}
			// 查询
			result = scanPayService.refundQuery(base);
			logger.error("ScanPayInteractServiceImpl退款查询返回base对象：" + ToStringBuilder.reflectionToString(result));
		} catch (GopayException e) {
			logger.error("ScanPayInteractServiceImpl refundQuery error: ", e);
			result.setResultCode(e.getErrCode());
			result.setSuccess(false);
		} catch (Exception e) {
			logger.error("ScanPayInteractServiceImpl refundQuery error: ", e);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100F1002);
			result.setSuccess(false);
		}
		return result;

	}

	@Override
	public Result verify(BaseMessage msg) throws GopayException {

		logger.info("------------ScanPayInteractServiceImpl verify start-------------");
		long beginTime = System.currentTimeMillis();

		ScanPayBaseMessage base = (ScanPayBaseMessage) msg;
		logger.error("ScanPayInteractServiceImpl 扫码支付verify 返回处理base对象：" + ToStringBuilder.reflectionToString(base));

		Result result = new Result();
		try {
			/**
			 * 没有直接返回订单ID业务处理
			 */
			if (base.getBankOrderId() == null || "".equals(base.getBankOrderId())) {
				base.setOutStlmCd(base.getBank());

				BaseBisBankExtConfig bankConfig = new BaseBisBankExtConfig();
				// 银行自动路由修改支付返回无订单号订单,获取产品渠道方法(由获取当前生效渠道改为根据银行、产品、渠道码获取) add by
				// zhengxue
				bankConfig = bisBankExtConfigManager.findExtConfigByBankChannelAndBankBusProd(base.getBankBusProd(),
						base.getBankIntrChann(), base.getBank());

				if (bankConfig != null) {
					// 设置一级私钥路径
					base.setPrivateKeyPath(bankConfig.getIntrMerPravkeyPath());
					// 设置一级私钥密码
					base.setPrivatePwd(bankConfig.getIntrMerPravkeyPwd());
					// 设置二级私钥路径
					base.setPrivateKeyPath2nd(bankConfig.getIntrMer2ndPravkeyPath());
					// 设置二级私钥密码
					base.setPrivatePwd2nd(bankConfig.getIntrMer2ndPravkeyPwd());
					// 设置公钥路径
					base.setPublicKeyPath(bankConfig.getIntrBankPubkeyPath());
				}

				/**
				 * 验签
				 */
				String beanName = getBankBeanName(base.getOutStlmCd(), bankConfig.getChCode(),
						base.getBankBusProd().value); 
				logger.error("密文银行获取beanName=" + beanName);

				ScanPayInteractRemoteService scanPayService = (ScanPayInteractRemoteService) SpringContextUtil
						.getBean(beanName);

				result = scanPayService.verify(base);

				base = (ScanPayBaseMessage) result.get("res");

				logger.error("ScanPayInteractServiceImpl 验签返回处理base对象：" + ToStringBuilder.reflectionToString(base));

				if (result.isSuccess() && (Boolean) result.getDefaultModel()) {
					// 验签成功
					if (result.isOrderStatus()) {

						bankOrderManager.updateBackSuccess(base.getOrderId());

						logger.error("银行【 " + beanName + "】返回处理结果：订单号【" + base.getOrderId()
								+ "】支付成功，bis_bank_main_order表更新成功状态");
					} else {
						logger.error("银行【 " + beanName + "】返回处理结果：订单号【" + base.getOrderId() + "】状态失败");
					}
				} else {
					// 验签失败
					bankOrderManager.updateBackVerifyFail(base.getOrderId());

					logger.error("银行【 " + beanName + "】返回处理结果：订单号【" + base.getOrderId()
							+ "】验签失败，bis_bank_main_order表更新失败状态");
				}

				BankOrder bankOrder = null;
				bankOrder = bankOrderManager.getBankOrderById(base.getBankOrderId());
				base.setOrderId(bankOrder.getOrderId());
				// 是否验签成功过
				if (StringUtils.equals(bankOrder.getOrderStatus(), BankOrderStatus.BANK_CALL_SUCCESS)) {
					result.setDefaultModel(true);
					result.setSuccess(true);
				}
				return result;
			}
			/**
			/**
			 * 返回订单ID业务处理
			 */
			BankOrder bankOrder = null;
			bankOrder = bankOrderManager
					.getBankOrderById(base.getBankOrderId() == null ? base.getOrderId() : base.getBankOrderId());
			String bankBizProdCode = bankOrder.getBankBizProdCode();
			base.setOrderId(bankOrder.getOrderId());

			if (BisBankBusinessProd._16.value.equals(bankBizProdCode)) {
				base.setBankBusProd(BisBankBusinessProd._16);
			} else if (BisBankBusinessProd._17.value.equals(bankBizProdCode)) {
				base.setBankBusProd(BisBankBusinessProd._17);
			} else {
				throw new GopayException(ProcCodeConstants.PROC_CODE_100E1141);
			}
            // 自动路由需使用主订单表对象,挪到此处获取 add by zhengxue
            CpsGenMainOrder mainOrder = new CpsGenMainOrder();
            // 获取订单信息与银行信息
            mainOrder = getMainOrder(bankOrder.getOrderId());

				base.setOutStlmCd(bankOrder.getBankCode());
				base.setCustomerID(mainOrder.getMerId());
				base.setBankIntrChann(BisBankInterfaceChannel.get(bankOrder.getInterfaceChennelCode()));
				base.setBank(bankOrder.getBankCode());
				BaseBisBankExtConfig bankConfig = new BaseBisBankExtConfig();
                if (isRouteOrWhiteList(mainOrder.getMerId())) {
                    // 增加银行自动路由开关 add by zhengxue
                    bankConfig = bisBankExtConfigManager.findExtConfigByBankChannelAndBankBusProd(base.getBankBusProd(), BisBankInterfaceChannel.get(bankOrder.getInterfaceChennelCode()), bankOrder.getBankCode());
                } else {
                    bankConfig = bisBankExtConfigManager.findExtConfigByBankCodeAndBankBusProd(base.getBankBusProd(), bankOrder.getBankCode());
                }
            	/**
				 * 根据银行订单号查询订单扩展表是否存在记录，存在记录获取支付使用的商户号和商户密钥
				 */
				getBisBankOrderExt(bankOrder.getOutOrderId(),bankConfig);
				//设置一级私钥路径
				base.setPrivateKeyPath(bankConfig.getIntrMerPravkeyPath());
				//设置一级私钥密码
				base.setPrivatePwd(bankConfig.getIntrMerPravkeyPwd());
				//设置二级私钥路径
				base.setPrivateKeyPath2nd(bankConfig.getIntrMer2ndPravkeyPath());
				//设置二级私钥密码
				base.setPrivatePwd2nd(bankConfig.getIntrMer2ndPravkeyPwd());
				//设置公钥路径
				base.setPublicKeyPath(bankConfig.getIntrBankPubkeyPath());
				String beanName = "";

				beanName = getBankBeanName(base.getOutStlmCd(), bankOrder.getInterfaceChennelCode(),
						base.getBankBusProd().value);
				logger.error("获取beanName=" + beanName);
				
				/**
				 * 国坛老窖商户先写死
				 */
				String custId=mainOrder.getMerId();
				logger.info("线上退款查询单独处理的商户号开始处理：="+custId);
				if (BisBankBusinessProd._16.value.equals(bankBizProdCode)
						&& bankOrder.getInterfaceChennelCode().equals(BisBankInterfaceChannel._15.value) && "0000053156".equals(custId)) {
					logger.info("线上退款查询单独处理的商户号开始处理：="+custId);
					// 设置商户号
					base.setMerID("102532000704");
					// 设置一级私钥路径
					base.setPrivateKeyPath("aa399c1537b47fd822dda95b904808c5");
				}
			/****************** 处理银行多次通知 begin ***********************/
			// 是否验签成功过
			if (StringUtils.equals(bankOrder.getOrderStatus(),BankOrderStatus.BANK_CALL_SUCCESS)) {				
				ScanPayInteractRemoteService scanPayService = (ScanPayInteractRemoteService) SpringContextUtil.getBean(beanName);               
				result = scanPayService.verify(base);
				result.setDefaultModel(true);
				result.setSuccess(true);
				return result;
			}
			// 是否验签失败过
			if (StringUtils.equals(bankOrder.getOrderStatus(),BankOrderStatus.VERIFY_FAIL)) {
				result.setDefaultModel(false);
				result.setSuccess(true);
				return result;
			}
			/************** 处理银行多次通知 end *******************/
			/**
			 * 验签开始
			 */
			ScanPayInteractRemoteService scanPayService = (ScanPayInteractRemoteService) SpringContextUtil.getBean(beanName);  
			result = scanPayService.verify(base);
			
			if (result.isSuccess() && (Boolean) result.getDefaultModel()) {
				/**
				 * 验签成功
				 */
				if (result.isOrderStatus()) {
					@SuppressWarnings("unchecked")
					Map<String, String> param = (Map<String, String>) result.getModel("isNotifyCashier");
					String amt = (String) param.get("amt");
					if (mainOrder.getTotalOrderAmt() != null
							&& mainOrder.getTotalOrderAmt().compareTo(new BigDecimal(amt)) != 0) {
						// 银行返回金额与平台上送金额不符
						logger.error("银行【 " + beanName + "】返回处理结果：国付宝订单号【" + base.getOrderId() + "】金额比对异常");
						return result;
					}
					bankOrderManager.updateBackSuccess(bankOrder.getOrderId());
					logger.error("银行【 "+beanName+"】返回处理结果：国付宝订单号【"+base.getOrderId()+"】支付成功，bis_bank_main_order表更新成功状态");
				} else {
					logger.error("银行【 "+beanName+"】返回处理结果：国付宝订单号【"+base.getOrderId()+"】验签失败，bis_bank_main_order表更新失败状态");
				}
			}else{
				// 验签失败
				bankOrderManager.updateBackVerifyFail(bankOrder.getOrderId());
				logger.error("银行【 "+beanName+"】返回处理结果：国付宝订单号【"+base.getOrderId()+"】验签失败，bis_bank_main_order表更新失败状态");
			}
			
		} catch (GopayException e) {
			logger.error("ScanPayInteractServiceImpl verify error: ", e);
			result.setResultCode(e.getErrCode());
			result.setSuccess(false);
		} catch (Exception e) {
			logger.error("ScanPayInteractServiceImpl verify error: ", e);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100F1002);
			result.setSuccess(false);
		}
		long endTime = System.currentTimeMillis();
		logger.info("------ ScanPayInteractServiceImpl verify end，耗时(ms):" + (endTime - beginTime));
		return result;

	}

	/**
	 * 
	 * 功能描述：校验请求参数
	 *
	 * @param request
	 *            请求对象
	 * @throws GopayException
	 * 
	 * @author 宣广海
	 *
	 * @since 2016年8月8日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	private void checkRequest(ScanPayRequest request) throws GopayException {
		// 订单号
		String orderId = request.getOrderId();
		// 终端IP
		String mchCreateIp = request.getMchCreateIp();
		// 是否支持信用卡
		String limitCreditPay = request.getLimitCreditPay();
		// 银行code
		String bank = request.getScanType();
		// 业务产品
		String bankBusProd = request.getBankBusProd().value;
		// 产品描述
		String goodsBody = request.getGoodsBody();
		logger.error("-----------bankBusProd----------------" + bankBusProd);
		if (StringUtils.isBlank(bankBusProd)) {
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E7014);
		}
		/**
		 * 三方APP支付
		 */
		if (BisBankBusinessProd._17.value.equals(bankBusProd)) {
			if (StringUtils.isBlank(request.getAppId())) {
				throw new GopayException(ProcCodeConstants.PROC_CODE_100E1156);
			}
		}
		if (StringUtils.isBlank(orderId)) {
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E1089);
		}
		if (StringUtils.isBlank(mchCreateIp)) {
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E1016);
		}
		if (StringUtils.isBlank(limitCreditPay)) {
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E7008);
		}
		if (StringUtils.isBlank(bank)) {
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E1135);
		}
		if (StringUtils.isBlank(goodsBody)) {
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E1083);
		}
	}

	/**
	 * 
	 * 功能描述：校验银行业务产品是否支持该通道
	 *
	 * @param config
	 * @param bankBusProd
	 * @throws GopayException
	 * 
	 * @author 宣广海
	 *
	 * @since 2016年8月8日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	private void checkBankBussProdAndIntrChan(BaseBisBankExtConfig config, String bankBusProd) throws GopayException {
		if (null == config) {
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E7021);
		}
		// 接口通道
		String intrChann = config.getChCode();
		bankIntrChannConfService.checkBankBussProdAndIntrChan(intrChann, bankBusProd);
	}

	/**
	 * 
	 * 功能描述：校验银行通道信息是否支持支付等交易
	 *
	 * @param config
	 * @param outTranCode
	 * @throws GopayException
	 * 
	 * @author 宣广海
	 *
	 * @since 2016年8月8日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	private void checkBankIntrChan(BaseBisBankConfig config, OutStlmTranCode outTranCode) throws GopayException {
		bankIntrChannConfService.checkBankIntrChan(config, outTranCode);
	}

	/**
	 * 
	 * 功能描述：获取订单信息
	 *
	 * @param orderId
	 * @return
	 * @throws GopayException
	 * 
	 * @author 宣广海
	 *
	 * @since 2016年8月8日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	private CpsGenMainOrder getMainOrder(String orderId) throws GopayException {
		if (orderId == null || "".equals(orderId)) {
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E5041);
		}
		// 获取主订单表
		CpsGenMainOrder mainOrder = mainOrderQuery.get(orderId);
		if (mainOrder == null) {
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E5041);
		}
		return mainOrder;
	}
	private BaseBisBankExtConfig getBisBankOrderExt(String bankOrderId,BaseBisBankExtConfig bankConfig) throws GopayException {
		if (bankOrderId == null || "".equals(bankOrderId)) {
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E5041);
		}
		BankOrderExt bankOrderExt =bankOrderManager.getBankOrderExtById(bankOrderId);
		if(null!=bankOrderExt){
			logger.error("-----------查询一户一码特有商户号为：---------------" + bankOrderExt.getMerchantId());
			bankConfig.setIntrMernum1(bankOrderExt.getMerchantId());
			bankConfig.setIntrMerPravkeyPath(bankOrderExt.getMerchantKey());
		}
		return bankConfig;
	}

	/**
	 * 获取SPRING加载的BeanName
	 * 
	 * <pre>
	 * BeanName规范:银行编码-交易类型，如TBANK-B2B
	 * </pre>
	 * 
	 * @param mainOrder
	 *            订单信息
	 * @param bankInfo
	 *            银行信息
	 * @return
	 * @throws GopayException
	 */
	private String getBeanName(CpsGenMainOrder mainOrder, String outStlmId, String inChann) throws GopayException {
		String beanSuffix = TradeType.getOrderTrandType(mainOrder.getPayChannel());
		if (StringUtils.equalsIgnoreCase(outStlmId, BankConstants.BANK_LOGO_WECHAT)
				&& StringUtils.equals(inChann, BisBankInterfaceChannel._16.value)) {
			String beanName = outStlmId + "-" + beanSuffix + "-" + BankConstants.BANK_LOGO_CIB;
			return beanName;
		}
		if (StringUtils.equals(inChann, BisBankInterfaceChannel._28.value)) {
			String beanName = outStlmId + "-" + beanSuffix + "-" + BankConstants.BANK_LOGO_SPDB;
			return beanName;
		}
		// add by jxj 添加对民生扫码支付的判断
		if (StringUtils.equalsIgnoreCase(outStlmId,  BankConstants.BANK_LOGO_WECHAT)
				&& StringUtils.equals(inChann, BisBankInterfaceChannel._17.value)) {
			String beanName = outStlmId + "-" + beanSuffix + "-" + BankConstants.BANK_LOGO_CMBC;
			return beanName;
		}
		if (StringUtils.equalsIgnoreCase(outStlmId,  BankConstants.BANK_LOGO_ALIPAY)
				&& StringUtils.equals(inChann, BisBankInterfaceChannel._17.value)) {
			String beanName = outStlmId + "-" + beanSuffix + "-" + BankConstants.BANK_LOGO_CMBC;
			return beanName;
		}
		if (StringUtils.equalsIgnoreCase(outStlmId,  BankConstants.BANK_LOGO_WECHAT)
				&& StringUtils.equals(inChann, BisBankInterfaceChannel._25.value)) {
			String beanName = outStlmId + "-" + beanSuffix + "-" + BankConstants.BANK_LOGO_PAB;
			return beanName;
		}
		if (StringUtils.equalsIgnoreCase(outStlmId, BankConstants.BANK_LOGO_ALIPAY)
				&& StringUtils.equals(inChann, BisBankInterfaceChannel._25.value)) {
			String beanName = outStlmId + "-" + beanSuffix + "-" + BankConstants.BANK_LOGO_PAB;
			return beanName;
		}
		String beanName = outStlmId + "-" + beanSuffix;
		return beanName;
	}

	/**
	 * 获取SPRING加载的BeanName(银行返回密文) BeanName规范:银行编码-交易类型，如TBANK-B2B
	 * 
	 * @param message
	 * @return String
	 * @throws GopayException
	 */
	private String getBankBeanName(String outStlmId, String inChann, String payChann) throws GopayException {
		String beanSuffix = TradeType.getOrderTrandType(payChann);

		// add by jxj 添加对民生扫码支付的判断
		if (StringUtils.equalsIgnoreCase(outStlmId,  BankConstants.BANK_LOGO_WECHAT)
				&& StringUtils.equals(inChann, BisBankInterfaceChannel._17.value)) {
			String beanName = outStlmId + "-" + beanSuffix + "-" + BankConstants.BANK_LOGO_CMBC;
			return beanName;
		}
		if (StringUtils.equalsIgnoreCase(outStlmId, BankConstants.BANK_LOGO_ALIPAY)
				&& StringUtils.equals(inChann, BisBankInterfaceChannel._17.value)) {
			String beanName = outStlmId + "-" + beanSuffix + "-" + BankConstants.BANK_LOGO_CMBC;
			return beanName;
		}
		if (StringUtils.equalsIgnoreCase(outStlmId,  BankConstants.BANK_LOGO_WECHAT)
				&& StringUtils.equals(inChann, BisBankInterfaceChannel._16.value)) {
			String beanName = outStlmId + "-" + beanSuffix + "-" + BankConstants.BANK_LOGO_CIB;
			return beanName;
		}
		if (StringUtils.equalsIgnoreCase(outStlmId,  BankConstants.BANK_LOGO_WECHAT)
				&& StringUtils.equals(inChann, BisBankInterfaceChannel._25.value)) {
			String beanName = outStlmId + "-" + beanSuffix + "-" + BankConstants.BANK_LOGO_PAB;
			return beanName;
		}
		if (StringUtils.equalsIgnoreCase(outStlmId,BankConstants.BANK_LOGO_ALIPAY)
				&& StringUtils.equals(inChann, BisBankInterfaceChannel._25.value)) {
			String beanName = outStlmId + "-" + beanSuffix + "-" + BankConstants.BANK_LOGO_PAB;
			return beanName;
		}
		if (StringUtils.equalsIgnoreCase(outStlmId, BankConstants.BANK_LOGO_WECHAT)
				&& StringUtils.equals(inChann, BisBankInterfaceChannel._28.value)){
			String beanName = outStlmId + "-" + beanSuffix + "-" + BankConstants.BANK_LOGO_SPDB;
			return beanName;
		}
		if (StringUtils.equalsIgnoreCase(outStlmId,BankConstants.BANK_LOGO_ALIPAY)
				&& StringUtils.equals(inChann, BisBankInterfaceChannel._28.value)) {
			String beanName = outStlmId + "-" + beanSuffix + "-" + BankConstants.BANK_LOGO_SPDB;
			return beanName;
		}
		String beanName = outStlmId + "-" + beanSuffix;
		return beanName;
	}



	private static String modifyOrderId(String orderId, String chann) {
		// 中信
		if (BisBankInterfaceChannel._15.value.equals(chann)) {
			orderId = "WF" + orderId.substring(2);
		}
		// 民生扫码
		if (BisBankInterfaceChannel._17.value.equals(chann)) {
			orderId = "MS" + orderId.substring(2);
		}
		// 兴业
		if (BisBankInterfaceChannel._16.value.equals(chann)) {
			orderId = "XY" + orderId.substring(2);
		}
		//平安
		if (BisBankInterfaceChannel._25.value.equals(chann)) {
			orderId = "PA" + orderId.substring(2);
		}
		// 浦发
		if (BisBankInterfaceChannel._28.value.equals(chann)) {
			orderId = "SP" + orderId.substring(2);
		}
		return orderId;
	}

	/**
	 * 
	 * 功能描述：是否已经发起退款交易
	 *
	 * @param oriOutOrderId
	 * @return
	 * 
	 * @author 宣广海
	 *
	 * @since 2016年10月17日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	private boolean isSendTkOrder(String oriOutOrderId) {
		boolean flag = true;
		// 根据原交易银行订单号获取退款订单
		List<BankOrder> tkBankOrders = bankOrderService.getBankOrdersByOriBankOrderId(oriOutOrderId);
		if (tkBankOrders != null && tkBankOrders.size() > 0) {
			// 同一支付订单下多笔退款时候, 前一笔退款状态为成功或者失败的情况下, 才允许做下一笔退款
			for (int i = 0; i < tkBankOrders.size(); i++) {
				String orderStatus = tkBankOrders.get(i).getOrderStatus();
				if (BankOrderStatus.PROCESS.equals(orderStatus)) {
					// 已存在处理中退款交易
					logger.error("ScanPayInteractServiceImpl refund isSendTkOrder: 已存在处理中退款交易 "
							+ tkBankOrders.get(i).getOrderId());
					flag = false;
					break;
				}
			}
		}
		return flag;
	}

	/**
	 * 是否为自动路由
	 * 
	 * @param merId
	 * @return
	 */
	private boolean isRouteOrWhiteList(String merId) {
		// 是否为白名单商户
		boolean isWhite = bisBankWhiteListMerchantsService.isWhiteListMerchant(merId);
		if (AUTO_ROUTING.equals(bisConfig.get("GOPAY.AUTO.ROUTING")) || isWhite) {
			return true;
		} else {
			return false;
		}
	}
}
